package com.exhr.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.exhr.dao.BaseDao;
import com.exhr.util.impl.Page;


public abstract class BaseDaoImpl<T> implements BaseDao<T>{
	
	protected HibernateTemplate hibernateTemplate;
	
	private Class<T> cla;
	
	public BaseDaoImpl(Class<T> cla){
		this.cla=cla;
	}
	
	public void add(T t){
		hibernateTemplate.save(t);
	}
	
	public void delete(T t){
		hibernateTemplate.delete(t);
	}
	
	public void delete(int id){
		T t=query(id);
		delete(t);
	}
	
	public void update(T t){
		hibernateTemplate.update(t);
	}
	
	public T query(int id){
		return (T)hibernateTemplate.get(cla, id);
	}
	
	public List<T> getAll(){
		return (List<T>)hibernateTemplate.find(" from "+cla.getSimpleName());
	}
	
	public List<T> getAll(final Page page){
		return (List<T>)hibernateTemplate.executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				
				String hql = " from "+cla.getSimpleName();

				Query query = session.createQuery(hql);

				// 总条数
				String counthql = "select count(e) from "+cla.getSimpleName()+" e";
				// uniqueResult ,返回一个对象
				Query query2 = session.createQuery(counthql);
				Integer count = ((Long) query2.uniqueResult())
						.intValue();
				page.setItemCount(count);

				// 分页信息
				// query.setMaxResults(20);//每页显示多少条数据
				// query.setFirstResult(0);//开始位

				return query.setMaxResults(page.getPageSize())
						.setFirstResult(page.getItemCount()).list();
				
			}
		});
	}
	
	protected List search(final String hql, final Object[] obs, final Page page) {
		return hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				//select id from object where id=？？？
				//select count(*)  object where id=？？？
				Query query = session.createQuery(hql);

				if (obs != null && obs.length > 0) {
					for (int i = 0; i < obs.length; i++) {
						query.setParameter(i + 1, obs[i]);
					}
				}
				
				//获得总条数
				page.setItemCount(getItemCount(hql,obs));

				query.setFirstResult(page.getCurrentItem()).setMaxResults(
						page.getPageSize());

				return query.list();
			}
		});
	}
	
	
	
	/**
	 * 获得总条数
	 * 
	 * @param sqlString查询语句
	 * @param values参数
	 * @return
	 */
	private Integer getItemCount(String hql, Object[] values) {

		String formatsql = "select count(*) ".concat(hql
				.substring(hql.indexOf("from")));

		if (hql.indexOf("order by") != -1) {
			formatsql = "select count(*) ".concat(hql.substring(hql
					.indexOf("from"), hql.indexOf("order by")));
		}

		return ((Long) queryObject(formatsql, values)).intValue();
	}
	
	
	/**
	 * 获得单个返回对象
	 * 
	 * @param sqlString
	 * @param values
	 * @return
	 */
	public Object queryObject(final String hql, final Object[] values) {
		return hibernateTemplate.execute(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				
				
				if (values != null && values.length > 0) {
					for (int i = 0; i < values.length; i++) {
						query.setParameter(i, values[i]);
					}
				}

				return query.uniqueResult();

			}

		});
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}


}

